package com.example.nna.voz;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.nna.main.R;
import com.example.nna.util.DBAdapter1;
import com.example.nna.util.Subscribe;
public class listViewCustom2 extends BaseAdapter{
	private ArrayList<containPage2> contains;
    private LayoutInflater inflater; 
    private ViewHolder holder;  
    private Handler mHandler ;
    private float mTextSize1;
    private float mTextSize2;
    private listViewCustom2 adapter;
    private Context context;
    private Subscribe mSubscribe;
	public listViewCustom2(Activity context,Handler handler,ArrayList<containPage2> contain) {  
       // super();         
        adapter = this;
        mSubscribe = new Subscribe(context);

        this.context = context;
        this.contains = contain;
        this.mHandler = handler;
        this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
	}  
	void setSize(float mTextSize){
	    TextView t1 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text1);
		TextView t2 = (TextView)inflater.inflate(R.layout.list_item, null).findViewById(R.id.text2);
		this.mTextSize1 = t1.getTextSize()*mTextSize;
		this.mTextSize2 = t2.getTextSize()*mTextSize;

	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contains.size(); 
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	public static class ViewHolder  
	{  
        ImageView imgView;  
        TextView txtView1;  
        TextView txtView2;
        ImageButton imagStar;
        RelativeLayout layout;
        ImageButton mGoto;
        ImageButton mFirst;
        ImageButton mLast;
        ImageButton mNext;
        ImageButton mPrev;
        ImageButton mGoto2;
        ImageButton mFirst2;
        ImageButton mLast2;
        ImageButton mNext2;
        ImageButton mPrev2;
	}  
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos;
		pos = position;
        if(convertView==null)  
        {  
            holder = new ViewHolder();  
            convertView = inflater.inflate(R.layout.list_item2, null);  
            holder.layout = (RelativeLayout) convertView.findViewById(R.id.listitem2);
            holder.imgView = (ImageView) convertView.findViewById(R.id.image);  
            holder.txtView1 = (TextView) convertView.findViewById(R.id.text1);  
            holder.txtView2 = (TextView) convertView.findViewById(R.id.text2);
            holder.imagStar = (ImageButton)convertView.findViewById(R.id.star);
            holder.mGoto = (ImageButton)convertView.findViewById(R.id.Page2Page);
            holder.mFirst = (ImageButton)convertView.findViewById(R.id.Page2First);
            holder.mLast= (ImageButton)convertView.findViewById(R.id.Page2Last);
            holder.mNext = (ImageButton)convertView.findViewById(R.id.Page2Next);
            holder.mPrev = (ImageButton)convertView.findViewById(R.id.Page2Previous);
            holder.mGoto2 = (ImageButton)convertView.findViewById(R.id.Page2Page2);
            holder.mFirst2 = (ImageButton)convertView.findViewById(R.id.Page2First2);
            holder.mLast2= (ImageButton)convertView.findViewById(R.id.Page2Last2);
            holder.mNext2 = (ImageButton)convertView.findViewById(R.id.Page2Next2);
            holder.mPrev2 = (ImageButton)convertView.findViewById(R.id.Page2Previous2);
            convertView.setTag(holder);  
        }  
        else  
            holder=(ViewHolder)convertView.getTag();  
        
        if(pos == 0) {
        	setVisible(View.VISIBLE);
        	setContainVisible(View.GONE);
        }else{
        	setVisible(View.GONE);
        	setContainVisible(View.VISIBLE);
        }
        if(pos == contains.size()-1) {
        	setVisible2(View.VISIBLE);
        	setContainVisible(View.GONE);
        }else{
        	setVisible2(View.GONE);
        }
        holder.mGoto.setOnClickListener(mGotoClick);
        holder.mFirst.setOnClickListener(mFirstClick);
        holder.mNext.setOnClickListener(mNextClick);
        holder.mLast.setOnClickListener(mLastClick);
        holder.mPrev.setOnClickListener(mPrevClick);
        holder.mGoto2.setOnClickListener(mGotoClick);
        holder.mFirst2.setOnClickListener(mFirstClick);
        holder.mNext2.setOnClickListener(mNextClick);
        holder.mLast2.setOnClickListener(mLastClick);
        holder.mPrev2.setOnClickListener(mPrevClick);
        
        if((contains.get(position).Image()==null)&&(contains.get(position).UrlThread()==null)){
        	holder.txtView1.setTextColor(context.getResources().getColor(R.color.white));
        	holder.layout.setBackgroundResource(R.color.vozTcat);
        	holder.txtView1.setText(contains.get(position).Thread()); 
        	holder.txtView2.setText("");  
        	holder.txtView2.setVisibility(View.GONE); 
        	holder.imagStar.setVisibility(View.GONE); 
        }else{
        	holder.txtView2.setVisibility(View.VISIBLE); 
        	holder.imagStar.setVisibility(View.VISIBLE); 
            holder.layout.setBackgroundResource(R.color.vozThread);
        	holder.txtView1.setTextColor(context.getResources().getColor(R.color.black));
        	holder.txtView2.setTextColor(context.getResources().getColor(R.color.black));
        	
        	if(contains.get(position).isSticky()) holder.txtView1.setTextColor(Color.RED);
        	else holder.txtView1.setTextColor(Color.BLACK);
        	
        	holder.txtView1.setText(contains.get(position).Thread());
            holder.txtView2.setText(contains.get(position).LastPost());  
           
        } 
             
        holder.txtView1.setTextSize(mTextSize1);
        holder.txtView2.setTextSize(mTextSize2);
        holder.imgView.setImageBitmap(contains.get(position).Image());
        holder.txtView1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(contains.get(pos).UrlThread().contains("forumdisplay")){
					mHandler.obtainMessage(Voz.MSG_PAGE2, pos).sendToTarget();
				}else
				mHandler.obtainMessage(Voz.MSG_PAGE3, contains.get(pos).UrlThread()).sendToTarget();
			}
		});
        if(contains.get(pos).UrlLastPosst() == null){
        	holder.imagStar.setVisibility(View.GONE);
        }else{
        	holder.imagStar.setVisibility(View.VISIBLE);
        }
        holder.imagStar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mHandler.obtainMessage(Voz.MSG_PAGE3, contains.get(pos).UrlLastPosst()).sendToTarget();
			}
		});
        return convertView;  
	}
	OnClickListener mGotoClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mHandler.obtainMessage(Voz.MSG_GOTO, 0).sendToTarget();
		}
	};
	OnClickListener mFirstClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mHandler.obtainMessage(Voz.MSG_GOTO, 1).sendToTarget();
		}
	};
	OnClickListener mNextClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mHandler.obtainMessage(Voz.MSG_GOTO, 3).sendToTarget();
		}
	};
	OnClickListener mLastClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mHandler.obtainMessage(Voz.MSG_GOTO, 4).sendToTarget();
		}
	};
	OnClickListener mPrevClick = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			mHandler.obtainMessage(Voz.MSG_GOTO, 2).sendToTarget();
		}
	};
	private void setContainVisible(int type){
        holder.imgView.setVisibility(type);
        holder.txtView1.setVisibility(type);  
        holder.txtView2.setVisibility(type);
        holder.imagStar.setVisibility(type);
	}
	private void setVisible(int type){
		holder.mGoto.setVisibility(type);
		holder.mFirst.setVisibility(type);
		holder.mLast.setVisibility(type);
		holder.mNext.setVisibility(type);
		holder.mPrev.setVisibility(type);
	}
	private void setVisible2(int type){
		holder.mGoto2.setVisibility(type);
		holder.mFirst2.setVisibility(type);
		holder.mLast2.setVisibility(type);
		holder.mNext2.setVisibility(type);
		holder.mPrev2.setVisibility(type);
	}
	
}
